﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivi.Visa.Interop;
using System.Data;

namespace SensitivityTest.model
{
    class FSL
    {
        private FormattedIO488 instrumentSpec = new FormattedIO488();//488接口
        private ResourceManager rm = new ResourceManager();
        public Boolean SpecInit(String sa, out string ErrStr)
        {
            try
            {
                instrumentSpec.IO = (IMessage)rm.Open(sa, AccessMode.NO_LOCK, 2000);
                instrumentSpec.IO.Timeout = 300000;
                instrumentSpec.WriteString("rst");
                ErrStr = "频谱仪连接成功";
                return true;
            }
            catch (Exception e)
            {
                ErrStr = "频谱仪连接失败" + e.Source + e.Message;
                return false;
            }
        }

        public void InitParamter_Spec(Frm_Main fmain)
        {
            instrumentSpec.WriteString("FREQ:CENT " + fmain.dt.Rows[0]["CenterFreq"] + "MHz");            //中心频点
            instrumentSpec.WriteString("FREQ:SPAN " + fmain.dt.Rows[0]["Span"] + "MHz");            //span
            instrumentSpec.WriteString(":BWID:RES " + fmain.dt.Rows[0]["RBW"] + "Hz");            //RBW
            instrumentSpec.WriteString(":BAND:VID " + fmain.dt.Rows[0]["VBW"] + "Hz");            //VBW
            instrumentSpec.WriteString("DISP:WIND:TRAC:Y:RLEV " + fmain.dt.Rows[0]["REF"] + "dBm");            //REF
            instrumentSpec.WriteString("INP:ATT " + fmain.dt.Rows[0]["Atten"] + "dB");            //Atten
            if (fmain.txt_Offset.Text != "")
            { 
                 instrumentSpec.WriteString("DISP:WIND:TRAC:Y:RLEV:OFFS " + fmain.txt_Offset.Text + "dB");            //线损补偿：Ampudut--more1of3-- Ref Lvl Offset 
            }
        }

        public void SetMark()
        {
            instrumentSpec.WriteString("CALC:MARK1:MAX");
        }
            
        
        public double GetMarkVal()
        {
            double ReadVal;
            instrumentSpec.WriteString("CALC:MARK1:Y?");
            ReadVal = double.Parse(instrumentSpec.ReadString());
            return ReadVal;
        }

        public void SetMarkNext()
        {
            instrumentSpec.WriteString("CALC:MARK1:MAX:next");
        }

        public void SetMarkLeft()
        {
            instrumentSpec.WriteString("CALC:MARK1:MAX:left");
        }

        public void SetConOn()
        {
            instrumentSpec.WriteString(":INIT:CONT 1");
            //System.Threading.Thread.Sleep(5000);
        }

        public void SingleSweep()
        { 
            instrumentSpec.WriteString("INITiate:CONTinuous OFF");
            instrumentSpec.WriteString ("INIT:IMM");
            GetOpc();
        }
        public string GetOpc()
        {
            string Dval;
            instrumentSpec.WriteString("*OPC?");
            Dval = instrumentSpec.ReadString();
            return Dval;
        }
       
           

    }
}
